package com.bug;

import com.bug.mamage.WordInfoManage;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class BugProcessor implements PageProcessor {

    private WordInfoManage wordInfoManage;
    private CatchStrategy catchStrategy;
    private Map<String, CatchStrategy.CatchPage> catchUrlMap = new HashMap();


    public BugProcessor(WordInfoManage wordInfoManage, CatchStrategy catchStrategy) {
        this.wordInfoManage = wordInfoManage;
        this.catchStrategy = catchStrategy;
    }

    @Override
    public void process(Page page) {
        // 执行对应的catch策略
        CatchStrategy.CatchPage catchPage = catchUrlMap.remove(page.getRequest().getUrl());
        if(catchPage != null) {
            Html html = page.getHtml();
            catchPage.setHtml(html);
            catchStrategy.catchPage(catchPage);
            // 谁才是最后执行的?

        }else {
            log.warn("not found {}", CatchStrategy.CatchPage.class.getSimpleName());
        }
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setSleepTime(100)
                .addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 Edg/81.0.416.72")
                .addHeader(":authority", "www.vocabulary.com")
                .addHeader(":method", "GET");
    }


    public void catchWord(String word) {
        // 获取一条URL爬取的对应信息
        List<CatchStrategy.CatchPage> catchPages = catchStrategy.getInfoCatchList(word);
        catchPages.forEach(catchPage -> {
            catchUrlMap.put(catchPage.getUrl(), catchPage);
            // 加入URL爬取
            wordInfoManage.getSpider().addUrl(catchPage.getUrl());
        });
    }



//    public static void main(String[] args) throws InterruptedException {
//        // 这里添加爬取策略(针对不同的网站)
//        BugProcessor processor = new BugProcessor();
//        Spider spider = Spider.create(processor).thread(10);
//        spider.setExitWhenComplete(false);
//        new Thread(()-> {
//            try {
//                TimeUnit.SECONDS.sleep(5);
//                spider.addRequest().addUrl("https://www.vocabulary.com/dictionary/definition.ajax?search=orange&lang=en");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }).start();
//        spider.run();
//
//    }
}
